package com.cy.redis;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.ResourceBundle;
 
public class RedisUtil {

    private static ResourceBundle resourceBundle;

    static {
        resourceBundle = ResourceBundle.getBundle("redis_config");

        initPool();
    }

    private static volatile JedisPool jedisPool;

    public static Jedis getResource() {
        return jedisPool.getResource();
    }

    public static void returnResource(Jedis jedis) {
        // jedisPool.returnResource(jedis);
        jedis.close();
    }

    public static void initPool() {
        if (jedisPool != null) {
            return;
        }
        String host = resourceBundle.getString("redis.host");
        int port = Integer.parseInt(resourceBundle.getString("redis.port"));
        JedisPoolConfig config = config();
        jedisPool = new JedisPool(config, host, port);
    }

    private static JedisPoolConfig config() {
        JedisPoolConfig config = new JedisPoolConfig();
        return config;
    }

    public static void activeUser(long userId, String dateKey) {
        Jedis jedis = RedisUtil.getResource();
        try {
            jedis.setbit(dateKey, userId, true);
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    //该天用户总数
    public static long totalCount(String dateKey) {
        Jedis jedis = RedisUtil.getResource();
        try {
            return jedis.bitcount(dateKey);
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    //该天登录所有的用户id
    public static List<Long> activeUserIds(String dateKey) {
        Jedis jedis = RedisUtil.getResource();
        try {

            if (jedis.get(dateKey) == null) {
                return null;
            }
            BitSet set = BitSetUtils.byteArray2BitSet(jedis.get(dateKey).getBytes());

            List<Long> list = new ArrayList<Long>();
            for (long i = 0; i < set.size(); i++) {
                if (set.get((int) i)) {
                    list.add(i);
                }
            }
            return list;
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    public static List<Long> continueActiveUserCount(String... dateKeys) {
        Jedis jedis = RedisUtil.getResource();
        try {
            BitSet all = null;
            for (String key : dateKeys) {
                if (jedis.get(key) == null) {
                    continue;
                }
                BitSet set = BitSetUtils.byteArray2BitSet(jedis.get(key).getBytes());
                if (all == null) {
                    all = set;
                }
                System.out.println(set.size());
                all.and(set);
            }
            List<Long> list = new ArrayList<Long>();
            for (long i = 0; i < all.size(); i++) {
                if (all.get((int) i)) {
                    list.add(i);
                }
            }
            return list;
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }


    public static void main(String[] args) {
        Jedis jedis = RedisUtil.getResource();
        RedisUtil.returnResource(jedis);

        System.out.println(jedisPool);

        activeUser(10003, "20180926");
        List<Long> list = activeUserIds("20180925");
        for (Long i : list) {
            System.out.println("the userid :" + i);
            System.out.println(totalCount("20180925"));
        }


    }

}